"""
MEMESA-TOOLS
============
Model preparation, vertex enumeration and vertex analysis tools developed as
part of the MEMESA project. 

    Copyright (C) 2009-2012 Brett G. Olivier and Steven M. Kelk

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>

Author: Brett G. Olivier
Contact email: bgoli@users.sourceforge.net

"""

import os, time, numpy
cDir = os.path.dirname(os.path.abspath(os.sys.argv[0]))

# Set userdata here #

# core_memesa_model
##  model_file = 'core_memesa_model.xml'
##  work_dir = 'core_memesa_model'
##  infinity_bound = 1000.0

# Ecoli_iJR904
model_file = 'Ecoli_iJR904.thr.xml'
work_dir = 'iJR904'
infinity_bound = 999999.0

# End userdata #

### Active script ###

model_name = model_file
model_dir = os.path.join(cDir, work_dir, 'sbml')
work_dir = os.path.join(cDir, work_dir, 'steven')

from pyscescbm.CBVersion import __DEBUG__, __version__
from pyscescbm import CBRead, CBWrite, CBTools
from pyscescbm import CBSolver as slv

work_dir = os.path.join(cDir, work_dir)
if not os.path.exists(work_dir):
    os.mkdir(work_dir)

mod = CBRead.readSBML2FBA(model_file, work_dir=model_dir)
mod.id = model_name

CBTools.processBiGGchemFormula(mod)
CBTools.processSBMLAnnotationNotes(mod, annotation_key='note')

print '\nAttempting to delete bounds for biomass reaction,', mod.getActiveObjective().getFluxObjectiveReactions()[0]
mod.deleteBoundsForReactionId(mod.getActiveObjective().getFluxObjectiveReactions()[0])

mLP = slv.analyzeModel(mod, return_lp_obj=True)
CBWrite.printFBASolution(mod)
tmp_mid = mod.id+'_cplex'
##  mod.id = mod.id+'_withinf'
##  CBWrite.exportModel(mod, fmt='hformat', work_dir=work_dir)

CBTools.countedPause(5)

mod.id = tmp_mid

print '\n%s\n' % len(mod.flux_bounds)
mod.changeAllFluxBoundsWithValue(infinity_bound, 'Infinity')
mod.changeAllFluxBoundsWithValue(-infinity_bound, '-Infinity')
mod.changeAllFluxBoundsWithValue(numpy.inf, 'Infinity')
mod.changeAllFluxBoundsWithValue(-numpy.inf, '-Infinity')

print '\n%s\n' % len(mod.flux_bounds)
slv.analyzeModel(mod, lpFname=os.path.join(work_dir, mod.id))
CBWrite.exportModel(mod, fmt='lp', work_dir=work_dir)

mod.id = mod.id.replace('_cplex','')+'.noinf'
print '\n%s\n' % len(mod.flux_bounds)
mod.deleteAllFluxBoundsWithValue('Infinity')
mod.deleteAllFluxBoundsWithValue('-Infinity')
print '\n%s\n' % len(mod.flux_bounds)
CBWrite.exportModel(mod, fmt='all', work_dir=work_dir)
